{include file="public/layout" /}

<body class="eyou_body_xin">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
{include file="system/left" /}
<div id="geduan_div" class="h15"></div>
<div class="page" style="min-width:auto;margin-left:115px;padding-bottom: 15px;">
    <div class="fixed-bar">
        <div class="item-title">
            <a class="back_xin" href="{:url('Shop/address_index')}" title="返回"><i class="iconfont e-fanhui"></i></a>
            <div class="subject">
                <h3>新增地址</h3>
                <h5></h5>
            </div>
        </div>
    </div>
    <form method="post" id="postForm" action="{:url('Shop/address_add')}" enctype="multipart/form-data" name="form1">
        <div class="ncap-form-default flexigrid htitx pt0">
            <dl class="row">
                <dt class="tit">
                    <label>退货地址名称</label>
                </dt>
                <dd class="opt">
                    <input name="title" class="input-txt" type="text" />
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>联系人</label>
                </dt>
                <dd class="opt">
                    <input name="contact" class="input-txt" type="text" />
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>手机号</label>
                </dt>
                <dd class="opt">
                    <input name="mobile" class="input-txt" type="text" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" />
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label>邮政编码</label>
                </dt>
                <dd class="opt">
                    <input name="postcode" class="input-txt" type="text" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');"/>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>所属地区</label>
                </dt>
                <dd class="opt">
                    <select name="province" id="province" onchange="changeRegion(this,1);">
                        {volist name="$region.province_list" id="vo"}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                    </select>
                    <select name="city" id="city" onchange="changeRegion(this,2);">
                        {volist name="$region.city_list" id="vo"}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                    </select>
                    <select name="area" id="area">
                        {volist name="$region.area_list" id="vo"}
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                    </select>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>详细地址</label>
                </dt>
                <dd class="opt">
                    <input name="address" class="input-txt" type="text" data-num="10" />
                </dd>
            </dl>
            <div class="bot">
                <a href="JavaScript:void(0);" class="ncap-btn-big ncap-btn-green" onclick="submit();">确认提交</a>
            </div>
        </div>
    </form>
</div>
{include file="public/footer" /}
<script type="text/javascript">
    try{
        var mt20_1649209614 = sessionStorage.getItem("mt20_1649209614");
        if (mt20_1649209614 == 1){
            $("#geduan_div").removeClass("h15");
        } else {
            $("#geduan_div").addClass("h15");
        }
    }catch(e){}
    function changeRegion(obj,level) {
        var id = $(obj).val();
        $.ajax({
            type : 'post',
            url : "{:url('Shop/get_region_list', [''=>1])}",
            data : {id:id,level:level,_ajax:1},
            dataType : 'json',
            success : function(res){
                if (res.data.city_list){
                    var html = '';
                    res.data.city_list.forEach(function( val, index ) {
                        html += '<option value="'+val.id+'">'+val.name+'</option>';
                    });
                    $('#city').html(html);
                }

                if (res.data.area_list){
                    var area_html = '';
                    res.data.area_list.forEach(function( val, index ) {
                        area_html += '<option value="'+val.id+'">'+val.name+'</option>';
                    });
                    $('#area').html(area_html);
                }
            },
            error: function(e){
                console.log(e)
            }
        });
    }

    function submit() {

        if ($("input[name=contact]").val() == '') {
            showErrorMsg('联系人不能为空');
            $("input[name=contact]").focus();
            return false;
        }
        if ($("input[name=mobile]").val() == '') {
            showErrorMsg('手机号不能为空');
            $("input[name=mobile]").focus();
            return false;
        }

        if ($("#province option:selected").val() == '' || $("#city option:selected").val() == '' || $("#area option:selected").val() == '') {
            showErrorMsg('请选择所属地区');
            return false;
        }
        if ($("input[name=address]").val() == '') {
            showErrorMsg('详细地址不能为空');
            $("input[name=address]").focus();
            return false;
        }

        layer_loading('正在处理');
        $.ajax({
            type : 'post',
            url : "{:url('Shop/address_add', ['_ajax'=>1])}",
            data : $('#postForm').serialize(),
            dataType : 'json',
            success : function(res){
                layer.closeAll();
                if(res.code == 1){
                    layer.msg(res.msg, {icon: 1, shade: layer_shade, time: 1000}, function(){
                        window.location.href = res.url;
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
</script>